package otherapi;

import clazz.TestOneUtils;
import entity.People;
import entity.User;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;

/**
 * @author 李栋
 * @version 1.0.0
 * @className other.OtherTest.java
 * @createTime 2021年06月10日 21:00:00
 */
public class OtherTest {

    private static final Logger logger = LoggerFactory.getLogger(OtherTest.class);

    @Test
    public void test3() {
        int a = testOne(3, 4);
        logger.info(String.valueOf(a));

    }

    @Test
    public void test4() {
        int[] arr = {5, 8, 10, 20, 43};
        arr[3] = arr[3] + arr[2];
        arr[2] = arr[3] - arr[2];
        arr[3] = arr[3] - arr[2];
        logger.info("we");

    }

    private int testOne(int a, int b) {
        if (a < b) {
            return testOne(b, a);
        } else if (b != 0) {
            return a + testOne(a, b - 1);
        } else {
            return 0;
        }
    }


    @Test
    public void test2() {
        List<String> list1 = Arrays.asList("A", "B", "C");
        List<String> list2 = Arrays.asList("A", "B", "C", "A", "B", "C", "A", "B");
        countVotes(list1, list2);

    }


    public static String countVotes(List<String> validCandidates, List<String> voteCasted) {
        List<Integer> list = new ArrayList<>();
        for (String s : validCandidates) {
            int count = 0;
            for (String m : voteCasted) {
                if (s.equals(m)) {
                    count++;
                }
            }
            list.add(count);
        }
        int max = 0;
        String name = null;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i) > max) {
                max = list.get(i);
                name = validCandidates.get(i);
            }
        }
        int min = list.get(0);
        String minName = null;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i) < min) {
                min = list.get(i);
                minName = validCandidates.get(i);
            }
        }
        return name + max + " " + minName + min;

    }


    public static List<Integer> swapAdjacent(List<Integer> array) {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < array.size(); i++) {
            if (i % 2 == 0) {
                try {
                    if ((i + 1) != array.size()) {
                        list.add(array.get(i + 1));
                    } else {
                        list.add(array.get(i));
                    }
                } catch (NullPointerException e) {
                }
            } else {
                list.add(array.get(i - 1));
            }
        }

        return list;
    }


    public static String RemoveLetters(String s, List<Character> letters) {
        StringBuilder str = new StringBuilder();
        for (char x : s.toCharArray()) {
            boolean flag = false;
            for (Character y : letters) {
                if (x == y) {
                    flag = true;
                }
            }
            if (!flag) {
                str.append(x);
            }
        }
        return str.toString();
    }


    @Test
    public void testGc() {
        for (int i = 0; i < 5; i++) {
            User username = new User();
        }
        System.gc();
        Runtime.getRuntime().gc();
    }


    @Test
    public void test5() {
        String strOne = "methodTest";
        String strTwo = "test2";
        logger.info("hash 值：{}", strOne.hashCode());
        logger.info("hash 值：{}", strTwo.hashCode());
        People peopleOne = new People(1, "张三", 23);
        People peopleTwo = new People(1, "张三", 23);
        logger.info("hash 值：{}", peopleOne.hashCode());
        logger.info("hash 值：{}", peopleTwo.hashCode());
    }

    @Test
    public void test6() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException {
        // Class clazz = Class.forName("clazz.TestOneUtils");
        Class clazz = TestOneUtils.class;

        // 对象的属性
        Field[] declaredFields = clazz.getDeclaredFields();

        // 对象的所有方法
        Method[] declaredMethods = clazz.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            declaredMethod.setAccessible(true);
            logger.info(String.valueOf(declaredMethods));
        }

        // 获取所有的构造方法(取不到)
        // Constructor<?> constructor = clazz.getConstructor();
        // 通过下面的方法能取到
        Constructor<TestOneUtils> declaredConstructor = clazz.getDeclaredConstructor(String.class);
        declaredConstructor.setAccessible(true);

        TestOneUtils testOne = (TestOneUtils) clazz.newInstance();
        testOne.testOne();

    }

    @Test
    public void test7() {
        // 左移
        for (int i = 0; i < 5; i++) {
            // 3 * 2的 i 次方
            logger.info("2 的" + i + "次方的数为：" + (3 << i));
        }
        // 右移
        for (int i = 0; i < 5; i++) {
            // 20 除以 2 的 i 次方
            logger.info("2 的" + i + "次方的数为：" + (20 >> i));
        }
    }


    @Test
    public void test8() {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(i);
        }
        // }
        // List<Map<String, Object>> list = new ArrayList<>();
        // Iterator<Map<String, Object>> iterator = labelAndInput.iterator();
        // int j = 0;
        // while (iterator.hasNext()) {
        //     Map<String, Object> map = iterator.next();
        //     Object o = map.get(Const.KEY_TYPE);
        //     // 偶数位如果不是  v_label 那么找到 v_label
        //     if ((j & 1) == 0 && !Const.V_LABEL.equals(o)) {
        //         Map<String, Object> temp = new HashMap<>(map);
        //         temp.put(Const.KEY_TYPE, Const.V_LABEL);
        //         list.add(temp);
        //         list.add(map);
        //         iterator.remove();
        //         j = j + 1;
        //     }
        //     j++;
        // }
        // labelAndInput.addAll(list);

    }

    @Test
    public void test9() {
        String a = "123";
        double b = Double.parseDouble(a);
        logger.info(String.valueOf(b));
        Map<String, Object> map = new HashMap<>();
        map.put(null, null);
        logger.info(map.toString());
    }

    @Test
    public void test10() {
        for (int i = 0; i < 10; i++) {
            if (i == 0) {
                continue;
            }
            logger.info(String.valueOf(i));
        }
    }

    @Test
    public void test() {
        for (int i = 0; i < 2; i++) {
            if (true) {
                logger.info("test");
                if (true) {
                    continue;
                }
                logger.info("test1");
            }
            logger.info("test2");
        }
    }

    @Test
    public void test1(){
        String s = UUID.randomUUID().toString();
        String tempStr = "afd0348c-15e2-4841-bbf5-efce3dfe8e51";
        UUID uuid = UUID.fromString(tempStr);
        System.out.printf("ok");
    }


}
